#include "DAOChapter.h"

DAOChapter::DAOChapter(const DBInfo& dbInfo)
{
  int result = 0;
  char *query=NULL;
  m_connection = mysql_init((MYSQL*) 0);
  if (m_connection != NULL && mysql_real_connect(m_connection,dbInfo.dbip.c_str(),dbInfo.dbuser.c_str(),dbInfo.dbpasswd.c_str(),dbInfo.dbname.c_str(),dbInfo.port,NULL,CLIENT_MULTI_STATEMENTS) )
  {
    if (!mysql_select_db(m_connection,dbInfo.dbname.c_str())) 
    {
      m_connection ->reconnect = 1; 
      query = "set names \'GBK\'";
      result = mysql_real_query(m_connection,query,strlen(query));
      if(result)
      {
        printf("[20100]Error making query: %s!", mysql_error(m_connection));
        //sprintf(m_logMsg, "[%s]Error making query: %s!", MSG_CHAPTERDAO_100, mysql_error(m_connection));
        //Log::error(m_logMsg);
      }
      else
      {
        m_sql = new char[SQL_BATCH_LENGTH];
        memset((void*)m_sql, 0x00, SQL_BATCH_LENGTH * sizeof(char));
      }
    }
  }
  else
  {
    printf("[20102]Error making query: %s!");
    //sprintf(m_logMsg, "[%s]Unable to connect the database,check your configuration!", MSG_CHAPTERDAO_102);
    //Log::error(m_logMsg);
  }
}

DAOChapter::~DAOChapter(void)
{
  if(NULL != m_connection)
  {
    mysql_close(m_connection);
    m_connection = NULL;
  }
  if(m_sql !=NULL)
  {
    delete[] m_sql;
    m_sql=NULL;
  }
}

bool DAOChapter::getChapterList(const Article& article, Chapter* chapterList, int& len)
{
  int result;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_CHAPTER_SELECT, article.articleid.c_str());
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_CHAPTERDAO_103, sql);
  //Log::debug(m_logMsg);
  //#endif
  result = mysql_real_query(m_connection, sql, strlen(sql));
  if (result)
  {
    printf("[20104]Query faild.%s \n", sql);
    return false;
  }
  m_res = mysql_store_result(m_connection);
  while ((m_row = mysql_fetch_row(m_res)) != 0)  
  {
    setFieldValue(chapterList);
    len++;
    chapterList++;
  }
  mysql_free_result(m_res);
  m_res = NULL;
  return true;
}

bool DAOChapter::getChapterListByTime(const string& lastupdate, const int max, Chapter* chapters, int& len)
{
  int result;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_CHAPTER_SELECT_TIME, lastupdate.c_str(), max);
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_CHAPTERDAO_105, sql);
  //Log::debug(m_logMsg);
  //#endif
  result = mysql_real_query(m_connection, sql, strlen(sql));
  if(result)
  {
    printf("[20106] get chapter list by time faild,%s", sql);
    //sprintf(m_logMsg, "[%s]Query faild!%s!", MSG_CHAPTERDAO_106, sql);
    //Log::error(m_logMsg);
    return false;
  }
  m_res = mysql_store_result(m_connection);
  while((m_row = mysql_fetch_row(m_res)) != 0)  
  {
    setFieldValue(chapters);
    len++;
    chapters++;
  }
  mysql_free_result(m_res);
  m_res = NULL;
  return true;
}

void DAOChapter::addChapterCache(const Chapter& chapter)
{
  char sql[SQL_LENGTH] = {0};
  sprintf(sql,SQL_CHAPTER_INSERT,
    chapter.chapterid.c_str(),chapter.siteid.c_str(),chapter.articleid.c_str(),chapter.articlename.c_str(),chapter.volumeid.c_str(),
    chapter.posterid.c_str(),chapter.poster.c_str(),chapter.postdate.c_str(),chapter.lastupdate.c_str(),chapter.chaptername.c_str(),
    chapter.chapterorder.c_str(),chapter.size.c_str(),chapter.saleprice.c_str(),chapter.salenum.c_str(),chapter.totalcost.c_str(),
    chapter.attachment.c_str(),chapter.isvip,chapter.chaptertype,chapter.power,chapter.display,chapter.txtlink.c_str());
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_CHAPTERDAO_107, sql);
  //Log::debug(m_logMsg);
  //#endif
  strcat(m_sql,sql);
}

bool DAOChapter::confirmCacheTran()
{
  int result = 0;   
  result = mysql_real_query(m_connection, m_sql,strlen(m_sql));
  do
  {
    m_res = mysql_store_result( m_connection );
    mysql_free_result(m_res);
    m_res = NULL;
  }while(!mysql_next_result( m_connection ) );
  memset((void*)m_sql,0x00, SQL_BATCH_LENGTH * sizeof(char));
  return !result;
}

string DAOChapter::getChapterMaxID()
{
  string result;
  int rt;
  char sql[SQL_LENGTH] = {0};
  sprintf(sql, SQL_CHAPTER_MAXID);
  //#ifdef _DEBUG
  //sprintf(m_logMsg, "[%s]%s!", MSG_CHAPTERDAO_108, sql);
  //Log::debug(m_logMsg);
  //#endif
  rt = mysql_real_query(m_connection, sql, strlen(sql));
  if(rt)
  {
    printf("[20109] get chapter max id faild");
    //#ifdef _DEBUG
    //sprintf(m_logMsg, "[%s]%s!", MSG_CHAPTERDAO_109, sql);
    //Log::debug(m_logMsg);
    //#endif
    return "";
  }
  m_res = mysql_store_result(m_connection);
  char tmp[12]={0};
  while((m_row = mysql_fetch_row(m_res)) != 0)  
  {
    if(m_row[0]!=NULL && strlen(m_row[0])>0)
    {
      sprintf(tmp,"%d",atoi(m_row[0])+1);
      result.assign(tmp);
    }
  }
  if(result.length() == 0)
  {
    result = "1";
  }
  mysql_free_result(m_res);
  m_res = NULL;
  return result;
}
